#!/usr/bin/env bash
GUEST_TIMEOUT=25
BUILD_DIR=$1
SCHED=$2
KERNEL=$3
VNG_RW_ARG=$4

if [ "${KERNEL}" == "vmlinuz" ]; then
  unset KERNEL
fi

VNG_RW=''
if [ "${VNG_RW_ARG}" == "VNG_RW=true" ]; then
  VNG_RW=' --rw '
fi

cd $BUILD_DIR || exit 1

if [ -n "${KERNEL}" ] && [ ! -x `which vng` ]; then
  echo "vng not found, please install virtme-ng to enable testing"
  exit 1
fi

if [ -n "${SCHED}" ]; then
  BPF_PATH=$(find ${BUILD_DIR} -type f -name bpf.bpf.o | grep ${SCHED})
  echo "Running veristat on ${BPF_PATH}"
  if [ -n "${KERNEL}" ]; then
    timeout --preserve-status ${GUEST_TIMEOUT} \
      vng --user root -m 10G --cpu 8 -v --user root -r ${KERNEL} -- \
        sudo veristat ${BPF_PATH} 2>&1 | tee veristat.ci.log
    exit $?
  else
    sudo veristat ${BPF_PATH} 2>&1 | tee veristat.ci.log
    exit $?
  fi
fi

for BPF_PATH in $(find ${BUILD_DIR} -type f -name bpf.bpf.o); do
  if [ -n "${KERNEL}" ]; then
    timeout --preserve-status ${GUEST_TIMEOUT} \
      vng --user root -m 10G --cpu 8 $VNG_RW -v --user root -r ${KERNEL} -- \
        sudo veristat ${BPF_PATH} 2>&1 | tee "$(basename "${BPF_PATH}")-veristat.ci.log"
  else
    echo "$BPF_PATH"
    sudo veristat ${BPF_PATH} 2>&1 | tee "$(basename "${BPF_PATH}")-veristat.ci.log"
  fi
done
